package net.hn.hnms.biz.performance.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.hn.hnms.biz.performance.domain.PerformanceTemplate;
import net.hn.hnms.biz.performance.mapper.PerformanceMetricsMapper;
import net.hn.hnms.biz.performance.mapper.PerformanceTemplateMapper;
import net.hn.hnms.biz.performance.request.PerformanceTemplateReq;
import net.hn.hnms.biz.performance.service.PerformanceMetricsService;
import net.hn.hnms.biz.performance.service.PerformanceTemplateService;
import net.hn.hnms.biz.performance.vo.PerformanceTemplateVo;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PerformanceTemplateServiceImpl extends BaseService<PerformanceTemplate, PerformanceTemplateVo> implements PerformanceTemplateService {


    @Autowired
    private PerformanceTemplateMapper performanceTemplateMapper;

    @Autowired
    private PerformanceMetricsService performanceMetricsService;


    @Autowired
    private PerformanceMetricsMapper performanceMetricsMapper;

    @Override
    public ResponsePage<PerformanceTemplateVo> selectPageList(PerformanceTemplateReq dimDrivageFace, PageQuery pageQuery) {
        Page<PerformanceTemplate> page = performanceTemplateMapper.selectPage(pageQuery.build(), buildQuery(dimDrivageFace).orderByDesc(PerformanceTemplate::getCreateTime));
        List<PerformanceTemplateVo> list = page.getRecords().stream().map(o -> convert(o, PerformanceTemplateVo.class)).collect(Collectors.toList());


        return new ResponsePage<>(list, page.getTotal());
    }



    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(List<String> idList) {
        performanceTemplateMapper.deleteBatchIds(idList);
    }


    @Override
    public void updateDeleteByIds(List<Integer> idList) {
        performanceTemplateMapper.updateDeleteByIds(idList);
    }

    @Override
    public int insert(PerformanceTemplateReq dimDrivageFace) {
        List<PerformanceTemplate> performanceTemplates = performanceTemplateMapper.selectList(buildQuery(dimDrivageFace));
        if (performanceTemplates.size()>0) {
            throw new HuaNengException(Constants.FAIL, "模板重复");
        }
//        String metricsDataJson = null;
//        ObjectMapper objectMapper = new ObjectMapper();
//        try {
//            metricsDataJson = objectMapper.writeValueAsString(dimDrivageFace.getMetricsData());
//
//        }catch (Exception e){
//            e.printStackTrace();
//        }

        PerformanceTemplate insertObj = new PerformanceTemplate();

        BeanUtil.copyProperties(dimDrivageFace, insertObj);
//        insertObj.setMetricsData(metricsDataJson);
        return performanceTemplateMapper.insert(insertObj);
    }

    @Override
    public PerformanceTemplateVo selectPerformanceTemplateById(Integer performanceTemplateId) {
        PerformanceTemplate performanceTemplate = performanceTemplateMapper.selectById(performanceTemplateId);
        if (performanceTemplate == null) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }

//        PerformanceMetricsReq performanceMetricsReq = new PerformanceMetricsReq();
//        performanceMetricsReq.setMetricsIds(performanceTemplate.getMetricsIds());
//        ResponsePage<PerformanceMetricsVo> performanceMetricsVoResponsePage = performanceMetricsService.selectPageList(performanceMetricsReq, null);
//        performanceTemplate.setMetricsList(performanceMetricsVoResponsePage);
        return convertWithUser(performanceTemplate, PerformanceTemplateVo.class);
    }

    @Override
    public void updateById(PerformanceTemplateReq dimDrivageFace) {
//        List<PerformanceTemplate> performanceTemplates = performanceTemplateMapper.selectList(buildQueryRepeat(dimDrivageFace));
//        if (performanceTemplates.size()>0) {
//            throw new HuaNengException(Constants.FAIL, "模板重复");
//        }
        PerformanceTemplate updateObj = new PerformanceTemplate();
        BeanUtil.copyProperties(dimDrivageFace, updateObj);
        int updateCount = performanceTemplateMapper.updateById(updateObj);
        if (updateCount == 0) {
            throw new HuaNengException(Constants.FAIL, "数据不存在");
        }
    }


    /**
     * 构造查询条件
     * @param req
     * @return LambdaQueryWrapper<DimDrivageFace> 查询条件
     */
    private LambdaQueryWrapper<PerformanceTemplate> buildQuery(PerformanceTemplateReq req){
        return new LambdaQueryWrapper<PerformanceTemplate>()
                .like(StrUtil.isNotBlank(req.getPerformanceTemplateName()), PerformanceTemplate::getPerformanceTemplateName, req.getPerformanceTemplateName());
    }


    private LambdaQueryWrapper<PerformanceTemplate> buildQueryRepeat(PerformanceTemplateReq req){
        return new LambdaQueryWrapper<PerformanceTemplate>()
                .eq(StrUtil.isNotBlank(req.getPerformanceTemplateName()), PerformanceTemplate::getPerformanceTemplateName, req.getPerformanceTemplateName())
                .ne(StrUtil.isNotBlank(req.getPerformanceTemplateId()), PerformanceTemplate::getPerformanceTemplateId, req.getPerformanceTemplateId());
    }
}
